// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Bootstrapper.cs" company="DatamineConsulting">
//   Copyright 2013 DatamineConsulting Ltd.
// </copyright>
// <summary>
//   Unity Inversion of control class
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Kanz4Kidz
{
    using System.Collections.Generic;
    using System.Web.Mvc;
    using Kanz4Kidz.Common;
    using Kanz4Kidz.Common.Interface;
    using Microsoft.Practices.Unity;
    using Unity.Mvc3;
    using RepositoryRegistration = Kanz4Kidz.Repository.RepositoryRegistration;
    using ServiceRegistration = Kanz4Kids.Service.Registration.ServiceRegistration;


    /// <summary>
    /// Unity Inversion of control class
    /// </summary>
    public static class Bootstrapper
    {
        /// <summary>
        /// Initializes this instance.
        /// </summary>
        public static void Initialize()
        {
            var container = BuildUnityContainer();

            DependencyResolver.SetResolver(new UnityDependencyResolver(container));
        }

        /// <summary>
        /// Builds the unity container.
        /// </summary>
        /// <returns>the unity container</returns>
        private static IUnityContainer BuildUnityContainer()
        {
            var container = Ioc.Container;

            IList<object> layersToRegister = new List<object>
                                                 {
                                                     new RepositoryRegistration(),
                                                     new ServiceRegistration()
                                                 };

            foreach (var layer in layersToRegister)
            {
                var registrationModule = layer as IRegistrationModule;
                if (registrationModule != null)
                {
                    registrationModule.RegisterDependencies(container);
                    registrationModule.RegisterMappings();
                }
            }

            return container;
        }
    }
}